/**
 * @author 20111008 by hzx
 * @class UserInfoSmsPanel
 * @extends Ext.grid.GridPanel
 * @description 用户信息列表
 */
UserInfoSmsPanel = Ext.extend(Ext.grid.GridPanel, {

			myid : "my_id",
//			addWin : null, // 参数,是AddUserInfoSmsWindow的引用
			editWin : null, // 参数,是EditUserInfoSmsWindow的引用
			editWinComm : null, // 参数,是EditUserInfoSmsCommWindow的引用
			chargeWin : null,//参数,充值窗口
			resetWin : null,//参数,充值窗口
			cur_amount : 0,
			cur_smscount : 0,
			cur_price : 0.1,
			cut : "cut",
			cui : "cui",
			cupi : "cupi",

			/**
			 * 构造方法
			 */
			constructor : function(_cfg) {
				if (_cfg == null) {
					_cfg = {};
				}

				Ext.apply(this, _cfg);
				this.addWin = null;//new AddBlackListWindow();
				
				this.editWin = new EditUserInfoSmsWindow();
				this.editWinComm = new EditUserInfoSmsCommWindow();

				// 数据存贮器
				this["store"] = new Ext.data.JsonStore({
							url : "userInfoSms.do?operator=showUserInfoSmsByPage",
							totalProperty : 'totalCount',
							root : 'rows',
							fields : ["userId","userName","userType","userTypeStr","parentId","sex","sexStr","telephone","cellphone","email","address","status","statusStr","balance","price","smscount","routeId","routeIdStr","checkFlag","checkFlagStr","feetype","feetypeStr","recordFlag","recordFlagStr","recvFlag","recvFlagStr","remark","lastLogin","createDate","createUserid","updateDate","updateUserid","discount_flag","discount"]
						});

				/**
				 * 拷贝父类的构造方法
				 */
				UserInfoSmsPanel.superclass.constructor.call(this, {
							id : this.myid,
							title:Ext.getCmp('MainTab').getActiveTab().title,
							border:false,
							stripeRows:true, //交替行效果
							iconCls:"houfei-treeNodeLeafIcon",
							viewConfig : {
								autoFill : true, //自动填充
								forceFit : true
							},
							colModel : new Ext.grid.ColumnModel([
									// 复选框列
									new Ext.grid.CheckboxSelectionModel({
												// 头
												header : ""
											}), 
									{
										// 指定表格的列的定义
										header : '用户ID',// 表头
										sortable: true,  //允许排序
										dataIndex : "userId"
									}, {
										header : '用户名称',// 表头
										sortable: true,  //允许排序
										dataIndex : "userName"
									}, {
										header : '用户类型',// 表头
										sortable: true,  //允许排序
										dataIndex : "userType",
										renderer: function(value) {
														if(value == '1') {
															return '管理员';
														}else if(value == '2') {
															return '代理用户';
														}else if(value == '3'){
															return '普通用户';
														}else{
															return value;
														}
													}

									}, {
										header : '上级用户',// 表头
										sortable: true,  //允许排序
										dataIndex : "parentId"
									}, {
										header : '联系手机',// 表头
										sortable: true,  //允许排序
										dataIndex : "cellphone"
									}, {
										header : '是否显示接收信息',// 表头
										sortable: true,  //允许排序
										dataIndex : "recvFlag",
										renderer: function(value) {
														if(value == '1') {
															return '显示';
														}else if(value == '2') {
															return '不显示';
														}else{
															return value;
														}
													},
										hidden : this.cut != "1"
									}, {
										header : '是否显示真实数据',
										sortable: true, 
										dataIndex : "recordFlag",
										renderer: function(value) {
														if(value == '1') {
															return '显示';
														}else if(value == '2') {
															return '不显示';
														}else{
															return value;
														}
													},
										hidden : this.cut != "1"
									}, {
										header : '是否需要审核',// 表头
										sortable: true,  //允许排序
										dataIndex : "checkFlagStr",
										hidden : this.cut != "1"
									}, {
										header : '最后登录时间',// 表头
										sortable: true,  //允许排序
										dataIndex : "lastLogin",
										width : 140
									}, {
										header : '状态',// 表头
										sortable: true,  //允许排序
										dataIndex : "status",
										width : 40,
										renderer: function(value) {
											if(value == '1') {
												return '正常';
											}else if(value == '2') {
												return '冻结';
											}else{
												return '正常';
											}
										}
									}, {
										header : '收费方式',// 表头
										sortable: true,  //允许排序
										dataIndex : "feetype",
										width : 125,
										renderer: function(value) {
														if(value == '1') {
															return '按状态报告收费';
														}else if(value == '2') {
															return '不按状态报告收费';
														}else{
															return value;
														}
													}
									}, {
										header : '扣量',// 表头
										sortable: true,  //允许排序
										dataIndex : "discount",
										hidden : this.cut != '1'
									}
									]),
							loadMask : {
								msg : "正在加载数据,请稍后..."
							},
							// 行选择模式
							selModel : new Ext.grid.RowSelectionModel({
										// 单行选择模式
										singleSelect : true
									}),
							tbar : [		
							    "用户id:",
							    {
									xtype : "textfield",
									name:"userId"
								},  "-",
								"用户名:",
							    {
									xtype : "textfield",
									name:"userName"
							    }, "-",
							    "手机号码:",
							    {
									xtype : "textfield",
									name:"mobile"
							    },"-",
							    {
									text : "查询",
									handler : this.onSearchUserInfoSms, // 添加角色事件
									iconCls : "houfei-searchIcon", // 按钮图片样式
									tooltip : "根据查询条件进行查询", // 提示信息
									scope : this
										// 范围
								},
								/*"-",
								{
								text : "新增",
								iconCls : "houfei-addIcon",
								handler : this.onAddUserInfoSms, // 编辑用户信息事件
								tooltip : "对选中的用户进行充值",
								scope : this,
								hidden :  this.cut != "1" //不是管理员 
								},*/
								"-" ,{
								text : "编辑",
								iconCls : "houfei-closeCurrentTab",
								handler : this.onEditUserInfoSms, // 编辑用户信息事件
								tooltip : "编辑选中的用户",
								scope : this
								}, "-", {
									text : "删除",
									iconCls : "houfei-closeCurrentTab",
									handler : this.onRemoveUserInfoSms, 
									tooltip : "删除选中的用户",
									scope : this,
									hidden : this.cut == "3" //普通用户
								},"-", {
									text : "冻结",
									iconCls : "houfei-closeCurrentTab",
									handler : this.onFreezeUserInfoSms, 
									tooltip : "冻结选中的用户",
									scope : this,
									hidden : this.cut == "3" //普通用户
								},"-", {
								text : "充值",
								iconCls : "houfei-refreshIcon",
								handler : this.onChargeRecord, // 编辑用户信息事件
								tooltip : "对选中的用户进行充值",
								scope : this,
								hidden : this.cut != "1" //不是管理员 
								},"-", {
									text : "激活",
									iconCls : "houfei-closeCurrentTab",
									handler : this.onUnFreezeUserInfoSms,
									tooltip : "激活选中的用户",
									scope : this,
									hidden : this.cut == "3" //普通用户
								},"-", {
									text : "重置密码",
									iconCls : "houfei-closeCurrentTab",
									handler : this.onRestPass, 
									tooltip : "重置选中的用户的密码",
									scope : this,
									hidden : this.cut == "3" //普通用户
								}, "-", {
								text : "刷新",
								iconCls : "houfei-refreshIcon",
								handler : this.onRefresh, // 刷新事件
								tooltip : "刷新列表信息",
								scope : this,
								hidden : true
							}],
							bbar : new Ext.PagingToolbar({
								store : this["store"], // 数据存储器
								pageSize : 20, // 页大小
								displayMsg:"共<font color=\"green\"> {2} </font>条记录,当前页记录索引<font color=\"green\"> {0} - {1}</font>&nbsp;&nbsp;&nbsp;",
								displayInfo:true
								})
						});
				// 数据加载
				this.getStore().load({
							params : { // 参数
								start : 0, // 索引
								limit : 20
								// 页大小
							}
						});
			},

			/**
			 * 表格数据刷新
			 */
			onRefresh : function() {
				this.getStore().reload();
			},
			/**
			 * 
			 * 创建一个新数据集
			 * 
			 * @param {}
			 *            _nowRecord 表格的当前选择行的record
			 */
			createNewRecord : function(_nowRecord) {
				var _record = new Ext.data.Record({
					"uis.userId" : _nowRecord.get("userId"), 
					"uis.userName" : _nowRecord.get("userName"),
					"uis.userType" : _nowRecord.get("userType"),
					"uis.userTypeStr" : _nowRecord.get("userTypeStr"),
					"uis.parentId" : _nowRecord.get("parentId"), 
					"uis.sex" : _nowRecord.get("sex"), 
					"uis.sexStr" : _nowRecord.get("sexStr"), 
					"uis.telephone" : _nowRecord.get("telephone"),
					"uis.cellphone" : _nowRecord.get("cellphone"),
					"uis.email" : _nowRecord.get("email"),
					"uis.address" : _nowRecord.get("address"),
					"uis.status" : _nowRecord.get("status"), 
					"uis.statusStr" : _nowRecord.get("statusStr"), 
					"uis.balance" : _nowRecord.get("balance"),
					"uis.price" : _nowRecord.get("price"),
					"uis.smscount" : _nowRecord.get("smscount"),
					"uis.routeId" : _nowRecord.get("routeId"), 
					"uis.routeIdStr" : _nowRecord.get("routeIdStr"), 
					"uis.checkFlagStr" : _nowRecord.get("checkFlagStr"),
					"uis.checkFlag" : _nowRecord.get("checkFlag"),
					"uis.feetype" : _nowRecord.get("feetype"),
					"uis.feetypeStr" : _nowRecord.get("feetypeStr"),
					"uis.recordFlag" : _nowRecord.get("recordFlag"),
					"uis.recordFlagStr" : _nowRecord.get("recordFlagStr"),
					"uis.recvFlag" : _nowRecord.get("recvFlag"),
					"uis.recvFlagStr" : _nowRecord.get("recvFlagStr"),
					"uis.remark" : _nowRecord.get("remark"),
					"uis.lastLoginStr" : _nowRecord.get("lastLogin"),
					"uis.createDateStr" : _nowRecord.get("createDate"),
					"uis.createUserid" : _nowRecord.get("createUserid"), 
					"uis.updateDateStr" : _nowRecord.get("updateDate"),
					"uis.updateUserid" : _nowRecord.get("updateUserid"),
					"uis.discount" : _nowRecord.get("discount"),
					"uis.oldMobile" : _nowRecord.get("cellphone"),
					"uis.discountFlag" : _nowRecord.get("discountFlag")
						});
				return _record;
			},
			createNewRecord2 : function(_nowRecord) {
				var _record = new Ext.data.Record({
					"uis.userId" : _nowRecord.get("userId"),
					"uis.balance" : _nowRecord.get("balance"),
					"uis.price" : _nowRecord.get("price"),
					"uis.smscount" : _nowRecord.get("smscount"),
					"uis.parentId" : _nowRecord.get("parentId")
						});
				return _record;
			},
			//充值事件
			onChargeRecord : function(){
				var _sm = this.getSelectionModel();
				// 当用户选择了一条记录的时候将编辑窗口显示出来"cellphone", "recvFlag","recordflag","checkFlag","lastLogin","status","feetype"
				if (_sm.getCount() > 0) {
					// 获取表格当前选择行的record
					var _nowRecord = _sm.getSelected();
					var _record = this.createNewRecord2(_nowRecord);
					this.cur_amount = _record.get("uis.balance");
					this.cur_price = _record.get("uis.price");
					this.cur_smscount = _record.get("uis.smscount");
					
					var _form = new ChargeRecordForm({
						_cur_amount : _record.get("uis.balance") , 
						_cur_price : _record.get("uis.price"), 
						_cur_smscount : _record.get("uis.smscount"),
						_cur_parentId : _nowRecord.get("uis.parentId"),
						record : _record
					}); 
					this.chargeWin = new ChargeRecordWindow({
						form : _form
					});

//					 显示窗体
					this.chargeWin.show();
				} else {
					Ext.Msg.show({
								title : "系统提示",
								msg : "请您选择一条您要充值的信息",
								buttons : Ext.Msg.OK,
								icon : Ext.Msg.INFO
							});
				}
				
			},
			//重置密码事件
			onRestPass : function(){
				var _sm = this.getSelectionModel();
				// 当用户选择了一条记录的时候将编辑窗口显示出来"cellphone", "recvFlag","recordflag","checkFlag","lastLogin","status","feetype"
				if (_sm.getCount() > 0) {
					// 获取表格当前选择行的record
					var _nowRecord = _sm.getSelected();
					var _record = this.createNewRecord2(_nowRecord);
					
					var _form = new ResetPassForm({
						record : _record
					}); 
					this.resetWin = new ResetPassWindow({
						form : _form
					});

					this.resetWin.show();
				} else {
					Ext.Msg.show({
						title : "系统提示",
						msg : "请您选择一条您要重置密码的信息",
						buttons : Ext.Msg.OK,
						icon : Ext.Msg.INFO
					});
				}
				
			},
			
			//新增用户
			onAddUserInfoSms : function() {
				//this.addWin.form.reset();
				//this.addWin.show();
			},
			
			/**
			 * 编辑事件
			 */
			onEditUserInfoSms : function() {
				var _sm = this.getSelectionModel();
				// 当用户选择了一条记录的时候将编辑窗口显示出来"cellphone", "recvFlag","recordflag","checkFlag","lastLogin","status","feetype"
				if (_sm.getCount() > 0) {
					// 获取表格当前选择行的record
					var _nowRecord = _sm.getSelected();
					// 重构一个新的record
					var _record = this.createNewRecord(_nowRecord);
					var _usertype = _nowRecord.get("userType");
					if(_usertype != "1" && this.cut == "2"){ 
						this.editWinComm.show();
						this.editWinComm.myForm.getForm().loadRecord(_record);
					}else{
						this.editWin.show();
						this.editWin.myForm.getForm().loadRecord(_record);
					}
						
				} else {
					Ext.Msg.show({
								title : "系统提示",
								msg : "请您选择一条您要修改的信息",
								buttons : Ext.Msg.OK,
								icon : Ext.Msg.INFO
							});
				}
			},
			
			/**
			 * 删除确认事件
			 */
			 onRemoveUserInfoSms : function() {
				var _sm = this.getSelectionModel();
				// 当用户选择了一条记录的时候将编辑窗口显示出来
				if (_sm.getCount() > 0) {
					Ext.Msg.confirm("系统消息", "您确定要删除当前选中的用户吗?",
							this.onDeleteConfrimBtnClick, this);
				} else {
					Ext.Msg.show({
								title : "系统提示",
								msg : "请您选择一条您要删除的信息",
								buttons : Ext.Msg.OK,
								icon : Ext.Msg.INFO
							});
				}
			},
			/**
			 * 删除事件
			 * 
			 */
			onDeleteConfrimBtnClick : function(_btn) {
				if (_btn == "yes") {
					// 获取当前表格的选择模式
					var _sm = this.getSelectionModel();
					// 获取表格当前选择行的record
					var _nowRecord = _sm.getSelected();
					// 重构一个新的record
					var _record = this.createNewRecord(_nowRecord);
					// 获取当前表格主键id
					var _id = _record.get("uis.userId");
					var _oldMobile = _record.get("uis.cellphone");
					
					if(_id == this.cui){
						Ext.Msg.alert("系统提示","你不能对自己进行删除!");
						return;
					}
					
					// 进行异步Ajax请求
					Ext.Ajax.request({
								url : "userInfoSms.do",
								method : "post",
								success : this.onSubmitSuccess,
								failure : this.onSubmitFailure,
								scope : this,
								params : {
									operator : "removeUserInfoSms",
									id : _id,
									oldMobile : _oldMobile
								}
							});
					/**
					 * 重新加载数据
					 */
					this.getStore().load({
								params : {
									start : 0,
									limit : 17
								}
							});
				}
			},
			/**
			 * 冻结确认事件
			 */
			 onFreezeUserInfoSms : function() {
				var _sm = this.getSelectionModel();
				// 当用户选择了一条记录的时候将编辑窗口显示出来
				if (_sm.getCount() > 0) {
					Ext.Msg.confirm("系统消息", "您确定要冻结当前选中的用户吗?",
							this.onFreezeConfrimBtnClick, this);
				} else {
					Ext.Msg.show({
						title : "系统提示",
						msg : "请您选择一条您要冻结的信息",
						buttons : Ext.Msg.OK,
						icon : Ext.Msg.INFO
					});
				}
			},
			/**
			* 冻结事件
			* 
			*/
			onFreezeConfrimBtnClick : function(_btn) {
				if (_btn == "yes") {
					// 获取当前表格的选择模式
					var _sm = this.getSelectionModel();
					// 获取表格当前选择行的record
					var _nowRecord = _sm.getSelected();
					// 重构一个新的record
					var _record = this.createNewRecord(_nowRecord);
					// 获取当前表格主键id
					var _id = _record.get("uis.userId");
					var _oldMobile = _record.get("uis.cellphone");
					var _parentId = _record.get("uis.parentId"); //取上级客户
					var _status = _record.get("uis.status"); //取客户的状态
					if(this.cut != "1" && (this.cut == "2" && this.cui != _parentId) ){
						Ext.Msg.alert("系统提示","您没有权限冻结该客户 !");
						return;
					}
					if(status =="2" ){
						Ext.Msg.alert("系统提示","该客户已处于冻结状态 !");
						return;
					}
					if(this.cui == _id ){
						Ext.Msg.alert("系统提示","不能冻结自己!");
						return;
					}
					
					// 进行异步Ajax请求
					Ext.Ajax.request({
						url : "userInfoSms.do",
						method : "post",
						success : this.onSubmitSuccess,
						failure : this.onSubmitFailure,
						scope : this,
						params : {
						operator : "freezeUserInfoSms",
						id : _id
					}
					});
					/**
					 * 重新加载数据
					 */
					this.getStore().load({
						params : {
						start : 0,
						limit : 17
					}
					});
				}
			},
			/**
			 * 激活确认事件
			 */
			 onUnFreezeUserInfoSms : function() {
				var _sm = this.getSelectionModel();
				// 当用户选择了一条记录的时候将编辑窗口显示出来
				if (_sm.getCount() > 0) {
					Ext.Msg.confirm("系统消息", "您确定要激活当前选中的用户吗?",
							this.onUnFreezeConfrimBtnClick, this);
				} else {
					Ext.Msg.show({
						title : "系统提示",
						msg : "请您选择一条您要激活的用户",
						buttons : Ext.Msg.OK,
						icon : Ext.Msg.INFO
					});
				}
			},
			/**
			* 激活事件
			* 
			*/
			onUnFreezeConfrimBtnClick : function(_btn) {
				if (_btn == "yes") {
					// 获取当前表格的选择模式
					var _sm = this.getSelectionModel();
					// 获取表格当前选择行的record
					var _nowRecord = _sm.getSelected();
					// 重构一个新的record
					var _record = this.createNewRecord(_nowRecord);
					// 获取当前表格主键id
					var _id = _record.get("uis.userId");
					var _oldMobile = _record.get("uis.cellphone");
					var _parentId = _record.get("uis.parentId"); //取上级客户
					var _status = _record.get("uis.status"); //取客户的状态
					if(this.cut != "1" && (this.cut == "2" && this.cui != _parentId) ){
						Ext.Msg.alert("系统提示","您没有权限激活该客户 !");
						return;
					}
					if(status =="2" ){
						Ext.Msg.alert("系统提示","该客户已处于激活状态 !");
						return;
					}
					if(this.cui == _id ){
						Ext.Msg.alert("系统提示","不能激活自己!");
						return;
					}
					
					// 进行异步Ajax请求
					Ext.Ajax.request({
						url : "userInfoSms.do",
						method : "post",
						success : this.onSubmitSuccess,
						failure : this.onSubmitFailure,
						scope : this,
						params : {
						operator : "unfreezeUserInfoSms",
						id : _id
					}
					});
					/**
					 * 重新加载数据
					 */
					this.getStore().load({
						params : {
						start : 0,
						limit : 17
					}
					});
				}
			},
			onSearchUserInfoSms : function() {
				// 获取top工具条
				var _tbar = this.getTopToolbar();
				// 获取text框的值
				var _userId = _tbar.items.get(1).getValue();
				var _userName = _tbar.items.get(4).getValue();
				var _mobile = _tbar.items.get(7).getValue();
				
				
				/**
				 * 给当前表格的数据源添加请求参数
				 */
				this.getStore().baseParams = {
					userId :_userId,
					userName : _userName,
					mobile : _mobile
				};
				/**
				 * 重新加载数据
				 */
				this.getStore().load({
							params : {
								start : 0,
								limit : 17
							}
						});
			},
			/**
			 * 请求成功的回调方法
			 * 
			 * @param {}
			 *            _form
			 * @param {}
			 *            _action
			 */
			onSubmitSuccess : function(resp, option) {
				var respText = Ext.util.JSON.decode(resp.responseText); 
				if(respText.success == true){
//					Ext.Msg.alert("系统消息","删除成功!");
					var respText = Ext.util.JSON.decode(resp.responseText); 
					Ext.Msg.alert("系统消息",respText.succMsg);
					/**
					 * _urgp 是 smsfactory.msg.batchSms.jsp
					 * 中WhiteListPanel类的实例
					 */
					_urgp.getStore().reload();
					
				}else{
					Ext.Msg.alert("系统消息",respText.errorMsg);
					_urgp.getStore().reload();
				}
			},
			/**
			 * 请求失败的回调方法
			 * 
			 * @param {}
			 *            _form
			 * @param {}
			 *            _action
			 */
			onSubmitFailure : function(resp, option) {
				var respText = Ext.util.JSON.decode(resp.responseText); 
				Ext.Msg.alert("系统消息",respText.errorMsg);
				_urgp.getStore().reload();
			}
			
		});